<?php
/**
 * employee.php
 * User: yzm
 * Date: 2017/8/24
 */

class Employee extends WebBase{
    //员工状态
    public $status = array(
        '1' => '已授权',
        '2' => '禁止登录',
    );

    public function __construct() {
        global $_W,$_GPC;
        $_W['employee_status'] = $this->status;
        parent::__construct();
    }

    /*
     * 员工管理列表
     */
    public function index() {
        global $_W,$_GPC;
        load()->func('tpl');

        if ($_W['isajax']) {
            $where_sql = ' WHERE `uniacid`=:uniacid ';
            $where[':uniacid'] = $_W['uniacid'];

            //筛选-姓名或者昵称
            if (!empty($_GPC['wx_name'])) {
                $where_sql .= ' AND (`nickname` LIKE :name OR `employee_name` LIKE :name ) ';
                $where[':name'] = "%".$_GPC['wx_name']."%";
            }
            //筛选-状态
            if (!empty($_GPC['status'])) {
                $where_sql .=' AND `status` = :status ';
                $where[':status'] = $_GPC['status'];
            }

            $total = pdo_fetchcolumn("SELECT count(*) FROM ".tablename('wherein_knowledge_employee') .$where_sql, $where);

            $list = pdo_fetchall("SELECT * FROM ".tablename('wherein_knowledge_employee') . $where_sql . " ORDER BY `add_time` DESC LIMIT ".$_GPC['offset'].",".$_GPC['limit'],$where);

            $out['total'] = $total;
            $out['rows'] = $list;
            $this->outjson($out);
        }

        include $this->template('index');
    }

    /*
     * 粉丝列表
     */
    public function fans() {
        global $_W,$_GPC;
        load()->model('mc');

        if ($_W['isajax']) {
            $fans_tag = mc_fans_groups(true);

            $where = array();
            $where[':uniacid'] = $_W['uniacid'];
            $where[':acid'] = $_W['acid'];

            $where_sql = " WHERE f.`uniacid` = :uniacid AND f.`acid` = :acid AND f.`follow` = '1' ";
            $orderby = " ORDER BY `followtime` DESC ";

            $tag = intval($_GPC['tag']) ? intval($_GPC['tag']) : 0;
            if (!empty($tag)) {
                $param[':tagid'] = $tag;
                $where_sql .= " AND m.`tagid` = :tagid";
            }

            //筛选-微信昵称
            if (!empty($_GPC['nickname'])) {
                $where_sql .=' AND f.`nickname` LIKE :nickname ';
                $where[':nickname'] = "%".addslashes(trim($_GPC['nickname']))."%";
            }
            //筛选-关注时间
            if (!empty($_GPC['followtime'])) {
                $where_sql .= ' AND (f.`followtime` >= :followtime AND f.`followtime` <= :end_time) ';
                $where[':followtime'] = strtotime($_GPC['followtime']);
                $where[':end_time'] = $where[':followtime'] + 24 * 60 * 60;
            }
            $count_select_sql = "SELECT %s FROM " .tablename('mc_mapping_fans')." AS f LEFT JOIN ".tablename('mc_fans_tag_mapping')." AS m ON m.`fanid` = f.`fanid` " . $where_sql;
            $count_fans_list_sql = sprintf($count_select_sql, "COUNT(f.`fanid`) ");
            $total = pdo_fetchcolumn($count_fans_list_sql, $where);

            $select_sql = "SELECT %s FROM " .tablename('mc_mapping_fans')." AS f LEFT JOIN ".tablename('mc_fans_tag_mapping')." AS m ON m.`fanid` = f.`fanid` " . $where_sql ." %s";
            $fans_list_sql = sprintf($select_sql, "f.* ", " GROUP BY f.`fanid`" . $orderby . " LIMIT " .$_GPC['offset'] . ",".$_GPC['limit']);
            $fans_list = pdo_fetchall($fans_list_sql, $where);

            if (!empty($fans_list)) {
                foreach ($fans_list as $k => $v) {
                    //查询用户是否已授权
                    $auth = pdo_get('wherein_knowledge_employee', array('uniacid' => $_W['uniacid'], 'openid' => $v['openid']), array('employee_id'));//status 是否授权 1已授权; 2禁止登录
                    $fans_list[$k]['is_auth'] = !empty($auth) ? 1 : 2;
                    if (!empty($v['tag']) && is_string($v['tag'])) {
                        if (is_base64($v['tag'])) {
                            $v['tag'] = base64_decode($v['tag']);
                        }
                        if (is_serialized($v['tag'])) {
                            $v['tag'] = @iunserializer($v['tag']);
                        }
                        $fans_list[$k]['avatar'] = empty($v['tag']['headimgurl']) ? KB_WEB_BACK_IMG . '/noavatar_middle.gif' : tomedia($v['tag']['headimgurl']);
                    }
                    $fans_list[$k]['followtime'] = format($v['followtime']);
                    unset($v);
                }
            }

            $out['total'] = $total;
            $out['rows'] = $fans_list;
            $this->outjson($out);
        }

        include $this->template('fans_list');
    }

    //编辑员工
    public function edit() {
        global $_W,$_GPC;

        if (checksubmit()) {
            $data = array();
            $data = array(
                'employee_name' => trim($_GPC['employee_name']),
                'mobile' => trim($_GPC['mobile']),
                'remarks' => htmlspecialchars_decode($_GPC['remarks'])
            );

            //编辑员工
            if (pdo_update('wherein_knowledge_employee',$data,array('employee_id' => $_GPC['employee_id']))) {
                $out['code'] = 200;
                $out['msg'] = '修改成功';
            } else {
                $out['code'] = 304;
                $out['msg'] = '没有可修改的数据';
            }
            $this->outjson($out);
        }
        //判断编辑还是添加
        if (!empty($_GPC['employee_id'])) {
            //编辑数据
            $item = pdo_get('wherein_knowledge_employee', array('employee_id' => $_GPC['employee_id']));
        }

        include $this->template('edit');
    }

    /*
     * 从粉丝列表处 添加员工--粉丝授权
     * 判断其中有些粉丝是否已在员工管理表里
     */
    public function add() {
        global $_W,$_GPC;

        if ($_W['isajax']) {
            $user = !empty($_GPC['user']) ? $_GPC['user'] : array();
            if (empty($user)) {
                $out['code'] = -1;
                $out['msg'] = '还没选中粉丝';
                $this->outjson($out);
            }

            $sql = "INSERT INTO " . tablename('wherein_knowledge_employee') . " (`uniacid`,`openid`,`nickname`,`status`,`add_time`) VALUES ";
            $str = '';
            $i = 0;
            //添加员工的个数
            $count_add_user = count($user);
            foreach ($user as $key => $value) {
                if ($i < $count_add_user) {
                    $str .= "(";
                    $str .= "'" . $_W['uniacid'] . "', '" . $key . "', '" . trim($value) . "', '1', '" . $_W['timestamp'] . "'";
                    $str .= "),";
                }
                $i++;
            }
            $sql .= rtrim($str, ',');

            if (pdo_query($sql)) {
                $out['code'] = 1;
                $out['msg'] = '授权成功';
            } else {
                $out['code'] = -1;
                $out['msg'] = '授权失败';
            }
            $this->outjson($out);
        }
        include $this->template('fans_list');
    }

    //ajax-删除员工
    public function del() {
        global $_GPC;
        if (isset($_GPC['multi']) && $_GPC['multi'] == '1') {
            //删除多个
            if (empty($_GPC['employee_id']) || !is_array($_GPC['employee_id'])) {
                $out['code'] = -1;
                $out['msg'] = '没有选中要删除的员工';
                $this->outjson($out);
            }
            $str_employee = implode(',', $_GPC['employee_id']);
            if (pdo_query("DELETE FROM ".tablename('wherein_knowledge_employee')." WHERE `employee_id` IN (" . $str_employee . ");")) {
                $out['code'] = 1;
                $out['msg'] = '删除成功';
            } else {
                $out['code'] = -1;
                $out['msg'] = '删除失败';
            }
        } else {
            //删除单个
            if (pdo_delete('wherein_knowledge_employee', array('employee_id' => $_GPC['employee_id']))) {
                $out['code'] = 1;
                $out['msg'] = '删除成功';
            } else {
                $out['code'] = -1;
                $out['msg'] = '删除失败';
            }
        }
        $this->outjson($out);
    }

    //ajax-给员工授权-改变员工登录状态
    public function auth() {
        global $_GPC;

        if (isset($_GPC['multi']) && $_GPC['multi'] == '1') {
            //授权多个
            if (empty($_GPC['employee_id']) || !is_array($_GPC['employee_id'])) {
                $out['code'] = -1;
                $out['msg'] = '没有选中授权员工';
            }

            $str_employee = implode(',', $_GPC['employee_id']);
            if (pdo_query("UPDATE ".tablename('wherein_knowledge_employee')." SET `status` = '1' WHERE `employee_id` IN (". $str_employee .")")) {
                $out['code'] = 1;
                $out['msg'] = '授权成功';
            } else {
                $out['code'] = -1;
                $out['msg'] = '授权失败: 没有可授权员工';
            }
        } else {
            //授权单个/取消单个授权
            if (pdo_update('wherein_knowledge_employee', array('status' => $_GPC['status']), array('employee_id' => $_GPC['employee_id']))) {
                $out['code'] = 1;
                $out['msg'] = ($_GPC['status'] == '1') ? '授权成功' : '取消授权成功';
            } else {
                $out['code'] = -1;
                $out['msg'] = '授权失败';
            }
        }
        $this->outjson($out);
    }


}